package com.cojson;

import java.io.IOException;
import java.io.InputStream;

public class Request {

    private InputStream inputStream;
    private  String uri;

    public Request(InputStream is){
        this.inputStream = is;
    }

    public void parse(){
        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try{
            i =  inputStream.read(buffer);
        }catch (IOException ex){
            ex.printStackTrace();
            i = -1;
        }
        for(int j = 0 ; j < i ;j++){
            request.append((char)buffer[j]);
        }
        String reqStr = request.toString();
        uri = parseUri(reqStr);
        System.out.println("Request: "+uri);
    }

    private String parseUri(String requestString){
        int index1,index2;
        index1 = requestString.indexOf(' ');
        if(index1 != -1){
            index2 = requestString.indexOf(' ',index1 + 1);
            if( index2 > index1){
                return requestString.substring(index1+1,index2);
            }
        }
        return null;
    }

    public String getUri(){
        return uri;
    }
}
